<template>
  <div class="app-container">
    <el-input
      class="marginRB10"
      v-model="listQuery.name"
      placeholder="请输入用户ID"
      style="width:200px;"
      clearable
    ></el-input>
    <el-input
      class="marginRB10"
      v-model="listQuery.name"
      placeholder="请输入昵称"
      style="width:200px;"
      clearable
    ></el-input>
    <el-input
      class="marginRB10"
      v-model="listQuery.name"
      placeholder="请输入邀请人昵称"
      style="width:200px;"
      clearable
    ></el-input>
    <el-button
      type="primary"
      @click="getList"
      icon="el-icon-search"
      class="marginRB10"
      >搜索</el-button
    >

    <el-table
      ref="dataList"
      :data="menuList"
      style="margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="用户id"> </el-table-column>
      <el-table-column prop="name" label="昵称"> </el-table-column>
      <el-table-column prop="price" label="邀请人id"> </el-table-column>
      <el-table-column prop="renew" label="邀请人昵称"> </el-table-column>
      <el-table-column prop="rebate" label="礼包名称"> </el-table-column>
      <el-table-column prop="rebate" label="礼包内容"> </el-table-column>
      <el-table-column prop="rebate" label="接收时间"> </el-table-column>

      <el-table-column min-width="180" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            plain
            >查看详情</el-button
          >
          <!--          <cus-del-btn @ok="handleDelete(scope.row)"></cus-del-btn>-->
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="form"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto;"
      >
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm()">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  saveData,
  deleteData,
  updateData,
  listData
} from "@/api/title/nobility";
export default {
  data() {
    return {
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10
      },
      menuList: [],
      dialogFormVisible: false,
      dialogStatus: "",
      titleMap: {
        update: "编辑",
        create: "增加"
      },
      form: {
        id: undefined,
        name: undefined
      },
      rules: {
        name: [{ required: true, message: "请输入名字", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      listData().then(response => {
        this.menuList = response.data;
        this.listLoading = false;
      });
    },
    handleCreate(row) {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      this.form = [];
      this.form.id = row.id;
      this.form.name = row.name;

      this.dialogStatus = "update";
      this.dialogFormVisible = true;

      this.form = Object.assign({}, this.form);
    },
    handleDelete(row) {
      let data = { ids: row.id };
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList();
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        }
      });
    },
    allDel() {

      let delNum = [];
      this.$refs.dataList.selection.forEach(k => {
        delNum.push(k.id);
      });
      if (delNum.length === 0) {
        return;
      }
      let data = { ids: delNum.toString() };
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList();
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        }
      });
    },
    submitForm() {
      this.$refs["dataForm"].validate(valid => {
        if (valid && this.dialogStatus === "create") {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined
              ? ""
              : formData.append(item, this.form[item]);
          }
          saveData(formData).then(response => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: response.message,
                type: "error"
              });
            }
          });
        } else if (valid && this.dialogStatus === "update") {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined
              ? ""
              : formData.append(item, this.form[item]);
          }
          updateData(formData).then(response => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: response.message,
                type: "error"
              });
            }
          });
        }
      });
    },
    resetForm() {
      this.form = {
        id: undefined,
        name: undefined
      };
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs["dataForm"]) {
        this.$refs["dataForm"].clearValidate(); // 清除整个表单的校验
      }
    }
  }
};
</script>
